import 'dart:ui';
import 'dart:ui' as ui;

///Canvas 扩展

extension CanvasExtends on Canvas {
  ///绘制圆角矩形
  ///[offset] 绘制左上角位置
  void drawImageAuto(
    ui.Image image, {
    Offset offset = Offset.zero,
    double? width,
    double? height,
    Paint? paint,
  }) {
    double dpr = ui.PlatformDispatcher.instance.views.first.devicePixelRatio;
    final imagePaint = paint ?? Paint()
      // ..colorFilter = ColorFilter.mode(Colors.green, BlendMode.srcIn)
      ..isAntiAlias = true
      ..filterQuality = FilterQuality.none;
    final dstWidth = width ?? image.width.toDouble() / dpr;
    final dstHeight = height ?? image.height.toDouble() / dpr;
    drawImageRect(
      image,
      //原图大小
      Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble()),
      //目标大小
      Rect.fromCenter(
        center: Offset(offset.dx + dstWidth / 2, offset.dy + dstHeight / 2),
        width: dstWidth,
        height: dstHeight,
      ),
      imagePaint,
    );
  }
}
